    <?php  
    class Mysql{  
             private  $host="localhost";  
             private  $user="root";  
             private  $password="root";  
             private  $dbName="";                     //数据库名  
             private  $charset="utf8";                    //字符编码  
             private  $port="3306";                       //端口号  
             private  $conn=null;
             private  $table;
             private  $fields=array();
             function __construct($db,$table){  
                 $this->table =$table;
                 $this->dbName=$db;
                 //连接数据库
                 $this->conn=new mysqli($this->host,$this->user,$this->password,$this->dbName,$this->port);  
                 if(!$this->conn)  
                 {  
                       die("数据库连接失败！".$this->conn->connect_error);  
                 }
                 $this->conn->query("set names ".$this->charset);  
                 $this->getFields();
             }  
             
             private function getFields(){
                $sql = "DESC ". $this->table;
                $result = $this->getAll($sql);
                foreach ($result as $v) {
                  $this->fields[] = $v['Field'];
                  if ($v['Key'] == 'PRI') {
                    //如果存在主键的话，则将其保存到变量$pk中
                    $pk = $v['Field'];
                  }
                }
                //如果存在主键，则将其加入到字段列表fields中
                if (isset($pk)) {
                  $this->fields['pk'] = $pk;
                }            
              }
             //执行sql语句  
              public function sql($sql){  
               $res=$this->conn->query($sql);  
                 if(!$res)  
                  {  
                       echo "数据操作失败";  
                  }else{  
                        //判断影响数据库条数
                     if($rows=$this->conn->affected_rows>0)  
                     {      //影响条数大于0,返回对象结果
                           return $res;  
                     }  
                     else  
                     {     //影响条数=0 输出
                          echo "0行数据受影响！";  
                     }  
                  }        
              }  
             public function update($list,$where){
                $uplist = ''; //更新列表字符串
                foreach ($list as $k => $v) {
                  if (in_array($k, $this->fields)) {
                      //拼接要修改的字段
                      $uplist .= "`$k`='$v'".",";
                  }
                }
                //去除uplist右边的
                $uplist = rtrim($uplist,',');
                //构造sql语句
                $sql = "UPDATE `{$this->table}` SET {$uplist} WHERE {$where}";
                
                $res =$this->sql($sql);
                return $res;
             }
    



             public function insert($list){
                $field_list = '';  //字段列表字符串
                $value_list = '';  //值列表字符串
                //循环输出提交结果分别拼接成字符串
                foreach ($list as $k => $v) {
                    if(in_array($k,$this->fields)){

                    $field_list .= "`".$k."`" . ',';
                    $value_list .= "'".$v."'" . ',';
                    }
                }
                //去除右边的逗号
                $field_list = rtrim($field_list,',');
                $value_list = rtrim($value_list,',');
                //构造sql语句
                $sql = "INSERT INTO `{$this->table}` ({$field_list}) VALUES ($value_list)";
                $res=$this->sql($sql);
                return $res;
               }
               
             public function delete($pk){
                if (is_array($pk)) {
                  # 数组
                  $where = "`{$this->fields['pk']}` in (".implode(',', $pk).")";
                } else {
                  # 单值
                  $where = "`{$this->fields['pk']}`=$pk";
                }
                //构造sql语句
                $sql = "DELETE FROM `{$this->table}` WHERE $where";
                //echo $sql;
                //exit;
                $res=$this->sql($sql);
                return $res;
            
            } 
            /**
             * 获取所有的记录
             * @access public 
             * @param $sql 执行的sql语句
             * @return $list 有所有记录组成的二维数组
             */
            public function getAll($sql){
               //调用sql方法执行sql语句
              $result = $this->sql($sql);
              $res = array();
              while ($row = $result->fetch_assoc()){
                $res[] = $row;
              }
              // 返回所有记录组成的二维数组
              return $res;
            } 

            public function getArr($sql,$fields){

               $result=$this->getAll($sql);
               $res=array();
              
               foreach ($result as $k =>$v){ 
       
                        $res[]=$v[$fields];                  
               }
                return $res;
            }

} 